﻿using AiShua.CookiePool.ApiModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace AiShua.CookiePool.Controllers
{
    public class CookieController : ApiController
    {
        DB.DB db = new DB.DB();
        /// <summary>
        /// 添加Cookie
        /// </summary>
        /// <param name="cookie"></param>
        /// <returns></returns>
        [HttpPost]
        public Msg<string> AddCookie(CookieModel cookie)
        {
            string msgstr = "";
            Msg<string> msg = new Msg<string>();
            msg.ret = -1;

            bool result = db.AddCookie(new Model.T_Cookie()
            {
                Cookie = cookie.cookie,
                ID = Guid.NewGuid().ToString(),
                PassWord = cookie.PassWord,
                UserName = cookie.UserName
            }, out msgstr);

            if (result)
            {
                msg.ret = 1;
            }

            return msg;
        }

        /// <summary>
        /// 根据手机号获取Cookie
        /// </summary>
        /// <param name="Phone"></param>
        [HttpPost]
        public Msg<ApiModel.CookieModel> GetCookie(string Phone)
        {
            string msgstr = "";
            Msg<ApiModel.CookieModel> msg = new Msg<ApiModel.CookieModel>();
            msg.ret = -1;
            Model.T_Cookie cookie = db.GetCookieByUserName(Phone, out msgstr);
            ApiModel.CookieModel apiCookie = new CookieModel()
            {
                cookie = cookie.Cookie,
                CookieType = "Web",
                IP = "",
                PassWord = cookie.UserName
            };
            if (cookie != null)
            {
                msg.data = apiCookie;
            }
            return msg;
        }

        public void GetRanDomCookie()
        {

        }

        /// <summary>
        /// 获取所有Cookie
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Msg<List<ApiModel.CookieModel>> GetAllCookie()
        {
            string msgstr = "";
            Msg<List<ApiModel.CookieModel>> msg = new Msg<List<ApiModel.CookieModel>>();
            msg.ret = -1;
            List<ApiModel.CookieModel> apiCookies = new List<CookieModel>();
            List<Model.T_Cookie> cookie = db.GetAllCookie(out msgstr);
            foreach (var item in cookie)
            {
                apiCookies.Add(new CookieModel()
                {
                    cookie = item.Cookie,
                    CookieType = "Web",
                    IP = "",
                    PassWord = item.UserName
                });
            }
            if (cookie != null)
            {
                msg.data = apiCookies;
            }
            return msg;
        }

        /// <summary>
        /// 获取Cookie数量
        /// </summary>
        [HttpPost]
        public Msg<int> CookieCount()
        {
            Msg<int> msg = new Msg<int>();
            msg.ret = 1;
            int count = db.GetCookieCount();
            msg.data = count;
            return msg;
        }
    }
}
